<?php

/**
 * @author Gideon Amissah<kuulmek@yahoo.com>
 */
class InfosController extends AppController {

    var $name = 'Infos';
    var $helpers = array('Form', 'Html', 'Session');
    var $components = array('RequestHandler', 'Session');
    # Models to be used
    var $uses = array(
        'Info',
        'TempScore'
    );

    function index() {
        
    }

    /**
     * This function register.
     * @name rigister
     * @access public
     */
    function register() {
        if (!empty($this->data)) {
            $result = $this->__beforeRegister();
            if ($result) {
                //echo debug($result);
                $this->Session->write('Client_id', $result['Info']['id']);
                $this->redirect(array('controller' => 'Questions', 'action' => 'instructions'), null, true);
                $this->Session->write('Client_data', $result);
                @mail($this->data['Info']['email'], 'PACEM Diagnostics Test Registration Notification', 
                            '<p>Dear Sir/Madam,</p>
                             
                             <p>This email is to notify you that you have just registered to take our 
                             PACEM Diagnostics test using your email address through which this message 
                             has been received.</p>

                             <p>In case you are not the one that used your email address for the purpose 
                             above, kindly inform us immediately using the contact details below.</p>
                             
                            <p><strong>Email Addresses</strong><br/>
                            Talktous@rtheconsult.com <br/>
                            Tathompson@rtheconsult.com<br/>
                            <strong>Phone Numbers</strong><br/>
                            Office lines - +233307033535, +233307033235 <br/>
                            Mobile line - +233248324101.</p><br/>

                            <p>Thank you.</p>

                            <p>RtHE Consult Ltd.</p>', "From: 'RtHE Consult',  <$this->data['Info']['email']>");
            } else {
                $this->Info->create();
                if ($this->Info->save($this->data)) {
                    $this->Session->setFlash('The Info has been saved');

                    $this->Session->write('Client_id', $this->Info->id);

                    @mail($this->data['Info']['email'], 'PACEM Diagnostics Test Registration Notification', 
                            '<p>Dear Sir/Madam,</p>
                             
                             <p>This email is to notify you that you have just registered to take our 
                             PACEM Diagnostics test using your email address through which this message 
                             has been received.</p>

                             <p>In case you are not the one that used your email address for the purpose 
                             above, kindly inform us immediately using the contact details below.</p>
                             
                            <p><strong>Email Addresses</strong><br/>
                            Talktous@rtheconsult.com <br/>
                            Tathompson@rtheconsult.com<br/>
                            <strong>Phone Numbers</strong><br/>
                            Office lines - +233307033535, +233307033235 <br/>
                            Mobile line - +233248324101.</p><br/>

                            <p>Thank you.</p>

                            <p>RtHE Consult Ltd.</p>', "From: 'RtHE Consult',  <$this->data['Info']['email']>");

                    $this->redirect(array('controller' => 'Questions', 'action' => 'instructions'), null, true);
                } elseif ($this->data == "") {
                    $this->Session->setFlash('Info not saved. Try again.');
                    $this->redirect('/');
                }
            }
        }
    }

    /**
     * @This function checks if the user has already registered then loads his data if any.
     * @name beforeRigister
     * @access public
     */
    function __beforeRegister() {
        $email = $this->data['Info']['email'];
        $result = $this->Info->find('first', array(
            'conditions' => array('Info.email' => $email),
            'contain' => array('TempScore'),
            'recursive' => 1
                ));
        return $result;
    }

}

?>
